/**
 * 公司管理服务
 * 提供公司管理相关功能
 */

const request = require('../libs/request.js')

const memberList = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/company/member-list', {
            data: params
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '获取员工列表失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '获取员工列表失败'
        }
    }
}

const removeMember = async (userId) => {
    try {
        const response = await request.authPost('/v1/company/remove-member', {
            data: {
                user_id: userId
            }
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '删除员工失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '删除员工失败'
        }
    }
}

const addMember = async (phone) => {
    try {
        const response = await request.authPost('/v1/company/add-member', {
            data: {
                phone: phone
            }
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '添加员工失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '添加员工失败'
        }
    }
}

/**
 * 获取我管理的公司的基本信息
 * @returns {Promise<{success: boolean, data?: object, message: string}>}
 */
const getMyCompanyInfo = async () => {
    try {
        const response = await request.authGet('/v1/company/get-my-company-info');
        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '获取公司信息失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '获取公司信息失败'
        }
    }
}

/**
 * 获取公司发布的所有卡片
 * @param {Object} params 查询参数
 * @param {number} params.page 页码，默认为1
 * @param {number} params.page_size 每页数量，默认为10
 * @returns {Promise<{success: boolean, data?: object, message: string}>}
 */
const getCompanyCards = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/company/get-company-cards', {
            data: params
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '获取公司卡片列表失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '获取公司卡片列表失败'
        }
    }
}

/**
 * 获取卡片激活记录明细
 * @param {Object} params 查询参数
 * @param {number} params.card_id 卡片ID
 * @param {number} params.page 页码，默认为1
 * @param {number} params.page_size 每页数量，默认为10
 * @returns {Promise<{success: boolean, data?: object, message: string}>}
 */
const getCardUserList = async (params = {}) => {
    try {
        const response = await request.authGet('/v1/company/get-card-user-list', {
            data: params
        });

        if (response.code === 1) {
            return {
                success: true,
                data: response.data,
                message: response.msg
            }
        } else {
            return {
                success: false,
                message: response.msg || '获取卡片激活记录失败'
            }
        }
    } catch (error) {
        return {
            success: false,
            message: error.message || '获取卡片激活记录失败'
        }
    }
}

module.exports = {
    memberList,
    removeMember,
    addMember,
    getMyCompanyInfo,
    getCompanyCards,
    getCardUserList
} 